package com.neusoft.elm;

import java.util.Scanner;

import com.neusoft.elm.po.Business;
import com.neusoft.elm.view.BusinessView;
import com.neusoft.elm.view.FoodView;
import com.neusoft.elm.view.impl.BusinessViewImpl;
import com.neusoft.elm.view.impl.FoodViewImpl;

public final class ElmBusinessEntry {
	
	public void work() {
		Scanner input = new Scanner(System.in);
		
		System.out.println("----------------------------------");
        System.out.println("|     欢迎下次光临饿了么后台管理平台                     |");
        System.out.println("----------------------------------");
		
		BusinessView businessView = new BusinessViewImpl();
		boolean loginSuccess = false;
		while (!loginSuccess) {
		Business business = businessView.login();
		
		if(business!=null) {
			loginSuccess = true;
			
			int menu = 0;
			while(menu!=5) {
				System.out.println("\n===== 一级菜单（商家管理） ======");
                 System.out.println("| 1. 查看商家信息                                  |");
                 System.out.println("| 2. 修改商家信息                                  |");
                 System.out.println("| 3. 更新密码                                          |");
                 System.out.println("| 4. 所属商品管理                                  |");
                 System.out.println("| 5. 退出系统                                          |");               
                 System.out.println("===========================");
                 System.out.print("请输入你的选择：");
                 
                 while (!input.hasNextInt()) {
	                System.out.println("输入无效，请重新输入数字选择：");
	                input.next(); 
	             }
				
                 menu = input.nextInt();
				
                 switch(menu) {
				    case 1 :
				    	businessView.showBusiness(business.getBusinessId());
				    	break;
				    case 2 :
				    	businessView.editBusiness(business.getBusinessId());
				    	break;
				    case 3 :
				    	businessView.updateBusinessPassword(business.getBusinessId());
				    	break;
				    case 4 :
				    	foodManager(business.getBusinessId());
				    	break;
				    case 5 :
				    	System.out.println("----------------------------------");
                        System.out.println("|     欢迎下次光临饿了么后台管理平台              |");
                        System.out.println("----------------------------------");
				    	break;
				    default:
				    	System.out.println("没有这个选项！\n");
				        break;	
				}
				
                 if (menu == 5) {
                    System.out.println("是否重新登录？(y/n)");
                    String choice = input.next();
                    if (choice.equals("y")) {
                        loginSuccess = false; 
                        break;
                    } else if (choice.equals("y")) {
                    	System.out.println("----------------------------------");
                        System.out.println("|     欢迎下次光临饿了么后台管理平台              |");
                        System.out.println("----------------------------------");
                        return;
                    } else {
                        System.out.println("输入无效！程序退出。");
                        return;
                    }
                }
				
				
			}
			
		}else {
			System.out.println("\n商家名称或密码输入错误!\n");
			System.out.println("请重新输入商家账户和密码！");}
		}
	  }
	
	private void foodManager(int businessId) {
		
		Scanner input = new Scanner(System.in);
		
		FoodView foodView = new FoodViewImpl();
		
		int menu = 0;
		while(menu!=6) {
			System.out.println("\n===== 二级菜单（食品管理） ======");
            System.out.println("| 1. 查看食品列表                                  |");
            System.out.println("| 2. 新增食品                                          |");
            System.out.println("| 3. 修改食品                                          |");
            System.out.println("| 4. 删除食品                                          |");
            System.out.println("| 5. 统计食品数量                                  |");
            System.out.println("| 6. 返回一级菜单                                  |");
            System.out.println("============================");
            System.out.print("请输入你的选择：");
			System.out.println("请输入你的选择：");
			while (!input.hasNextInt()) {
                System.out.println("输入无效，请重新输入数字选择：");
                input.next(); 
             }
			
			menu = input.nextInt();
			
			switch(menu) {
			    case 1 :
			    	foodView.showFoodList(businessId);
			    	break;
			    case 2 :
			    	foodView.saveFood(businessId);
			    	break;
			    case 3 :
			    	foodView.updateFood(businessId);	
			    	break;
			    case 4 :
			    	foodView.removeFood(businessId);
			    	break;
			    case 5:
			    	foodView.countFood(businessId);
			    	break;
			    case 6 :
			    	break;
			    default:
			    	System.out.println("没有这个选项！\n");
			        break;	
			}
	     }	
	  }
	
	  public static void main(String[] args) {
		  new ElmBusinessEntry().work();
	  }
   }
